module controller.AnimationVlinkController;

import std.json : JSONValue, parseJSON;
import std.conv : to;

import archttp.HttpStatusCode : HttpStatusCode;
import archttp.HttpRequest : HttpRequest;
import archttp.HttpResponse : HttpResponse;

import model.AnimationVlink;
import repository.AnimationLinksRepo;
import utils.RespData : RespData;
import utils.ObjMapping : ObjMapping;
import utils.SecurityVerification : signValidator;

/** 
 * 电视剧视频链接控制器
 */
class AnimationVlinkController
{

    AnimationLinksRepo _alrepo;
    this ()
    {
        _alrepo = new AnimationLinksRepo();
    }

    /** 
     * 获取一个电视剧视频链接
     */
    public void getOne (HttpRequest req, HttpResponse resp)
    {
        string idStr = req.param("id");
        if (idStr == "")
        {
            resp.json(RespData.newData(400, "错误的请求")).send();
        }
        uint id = to!uint(idStr);
        AnimationVlink alink = _alrepo.getOneAnimationLink(id);
        if (alink is null)
        {
            resp.code(HttpStatusCode.NOT_FOUND).json(RespData.newData(404, "未找到")).send();
            return;
        }

        JSONValue respData = RespData.newData!(AnimationVlinkDto)(200, "ok", ObjMapping.mappingToObject!(
                AnimationVlinkDto, AnimationVlink)(alink));
        resp.json(respData).send();
    }

    /** 
     * 添加链接
     */
    public void addLink (HttpRequest req, HttpResponse resp)
    {
        JSONValue json = parseJSON(req.body());
        string animation_id = json["animation_id"].str;
        string name = json["name"].str;
        string url = json["url"].str;
        string real_url = json["real_url"].str;

        string sign = json["sign"].str;

        import std.format : format;

        string reqParamsStr = format("%s%s", name, url);
        if (!signValidator(reqParamsStr, sign))
        {
            resp.code(HttpStatusCode.FORBIDDEN).json(RespData.newData(403, "签名不正确！")).send();
            return;
        }

        AnimationVlinkDtoFromCreate al = new AnimationVlinkDtoFromCreate(animation_id, name, url, real_url);

        bool res = _alrepo.addAnimationLink(ObjMapping.mappingToObject!(AnimationVlink, AnimationVlinkDtoFromCreate)(
                al));
        if (!res)
        {
            resp.code(HttpStatusCode.INTERNAL_SERVER_ERROR)
                .json(RespData.newData(500, "error")).send();
            return;
        }
        resp.code(HttpStatusCode.CREATED).json(RespData.newData(200, "ok")).send();
    }

    /** 
     * 删除一个链接
     */
    public void deleteLink (HttpRequest req, HttpResponse resp)
    {
        string idStr = req.param("id");
        string sign = req.query("sign");
        if (idStr == "" || sign is null || sign == "")
        {
            resp.json(RespData.newData(400, "错误的请求")).send();
            return;
        }
        uint id = to!uint(idStr);

        if (!signValidator(idStr, sign))
        {
            resp.code(HttpStatusCode.FORBIDDEN).json(RespData.newData(403, "签名不正确！")).send();
            return;
        }

        bool res = _alrepo.removeAnimationLink(id);
        if (!res)
        {
            resp.code(HttpStatusCode.INTERNAL_SERVER_ERROR)
                .json(RespData.newData(500, "error")).send();
            return;
        }
        resp.json(RespData.newData(200, "ok")).send();
    }

    /** 
     * 编辑链接
     */
    public void editLink (HttpRequest req, HttpResponse resp)
    {
        string idStr = req.param("id");
        if (idStr == "")
        {
            resp.json(RespData.newData(400, "错误的请求")).send();
        }
        uint id = to!uint(idStr);

        JSONValue json = parseJSON(req.body());
        string name = json["name"].str;
        string url = json["url"].str;
        string real_url = json["real_url"].str;

        string sign = json["sign"].str;

        import std.format : format;

        string reqParamsStr = format("%s%s", name, url);
        if (!signValidator(reqParamsStr, sign))
        {
            resp.code(HttpStatusCode.FORBIDDEN).json(RespData.newData(403, "签名不正确！")).send();
            return;
        }

        AnimationVlinkDtoFromUpdate al = new AnimationVlinkDtoFromUpdate(name, url, real_url);

        bool res = _alrepo.editAnimationLink(id, ObjMapping.mappingToObject!(AnimationVlink, AnimationVlinkDtoFromUpdate)(
                al));
        if (!res)
        {
            resp.code(HttpStatusCode.INTERNAL_SERVER_ERROR)
                .json(RespData.newData(500, "error")).send();
            return;
        }
        resp.json(RespData.newData(200, "ok")).send();
    }
}
